﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;


namespace DAO
{
    public class DichVu_DAO
    {
        private static DichVu_DAO m_instance = null;

        public static DichVu_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new DichVu_DAO();
                return m_instance;
            }
        }

        /// <summary>
        /// LẤY TOÀN BỘ DỊCH VỤ
        /// </summary>
        /// <returns></returns>
        public DataTable LayToanBoDichVu()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_TOANBO_DICHVU", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }


        /// <summary>
        /// THÊM DỊCH VỤ VÀO CSDL
        /// </summary>
        /// <returns></returns>
        public bool ThemDichVuMoi(string tendv, long dongia)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_DICHVU_MOI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@tendichvu", SqlDbType.NVarChar, 40);
                cmd.Parameters["@tendichvu"].Value = tendv;
                cmd.Parameters.Add("@dongia", SqlDbType.Money);
                cmd.Parameters["@dongia"].Value = dongia;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        /// <summary>
        /// Cập nhật dịch vụ
        /// </summary>
        /// <param name="madv"></param>
        /// <param name="tendv"></param>
        /// <param name="dongia"></param>
        /// <returns></returns>
        public bool CapNhatDichVu(string madv, string tendv, long dongia)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("UPDATE_DICHVU", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaDV", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaDV"].Value = madv;
                cmd.Parameters.Add("@TenDV", SqlDbType.NVarChar, 40);
                cmd.Parameters["@TenDV"].Value = tendv;
                cmd.Parameters.Add("@dongia", SqlDbType.Money);
                cmd.Parameters["@dongia"].Value = dongia;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        /// <summary>
        /// Xóa dịch vụ
        /// </summary>
        /// <returns></returns>
        public bool XoaDichVu(string madv)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("REMOVE_DICHVU", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaDV", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaDV"].Value = madv;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Lấy toàn bộ mã dịch vụ
        /// </summary>
        /// <returns></returns>
        public DataTable LayToanBoMaDichVu()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_TOANBO_MADICHVU", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }


        /// <summary>
        /// Lấy thông tin dịch vụ
        /// </summary>
        /// <param name="madv"></param>
        /// <returns></returns>
        public DataTable LayThongTinDichVu(string madv)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYDICHVUBYID", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaDV", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaDV"].Value = madv;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
